package com.vincent.service;

import com.vincent.common.enums.ResultMapKey;
import com.vincent.dao.AdDao;
import com.vincent.dao.AdPostionDao;
import com.vincent.dao.ArticleCategoryDao;
import com.vincent.dao.ArticleDao;
import com.vincent.model.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Vincent on 2014/12/20.
 */
@Service("homePageService")
public class HomePageService {
    @Resource
    private AdDao adDao;
    @Resource
    private AdPostionDao adPostionDao;
    @Resource
    private ArticleCategoryDao articleCategoryDao;
    @Resource
    private ArticleDao articleDao;


    public Map<String, Object> findHomePageShow() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //获取显示广告
        List<Ad> adList = adDao.selectAll();
        //显示广告位置
        List<AdPosition> adPositionList = adPostionDao.selectAll();
        for(AdPosition adPosition:adPositionList){
            List<Ad> ads=new ArrayList<Ad>();
            for(Ad ad:adList){
                if(ad.getPostion().intValue()==ad.getId().intValue()){
                    ads.add(ad);
                }
            }
            adPosition.setAdChildren(ads);
        }
        Map<String,Object> adMap=new HashMap<String, Object>();
        adMap.put("ads",adList);
        adMap.put("adposition",adPositionList);

        //首页文章显示
        List<ArticleCategory> categories = articleCategoryDao.selectAll();
        for(ArticleCategory category:categories){
            List<Integer> articleIdList = articleDao.selectTopByCategoryId(category.getId(), 5,true);
            List<Article> articleList =new ArrayList<Article>();
            for(Integer id:articleIdList){
                ArticleWithBLOBs articleWithBLOBs = articleDao.getArticleById(id);
                articleList.add(articleWithBLOBs);
            }
            category.setArticleList(articleList);
        }

        //数据对象
        Map<String,Object> dataMap=new HashMap<String, Object>();
        dataMap.put("adMap",adMap);
        dataMap.put("articles",categories);

        resultMap.put(ResultMapKey.SUCCESS.getCode(),dataMap);
        return resultMap;
    }
}
